import type { Directive } from "vue";
// 引入重置样式
import "./styles/reset.scss";
// 引入公共样式
import "./styles/index.scss";
// 引入tailwind样式
import "./styles/tailwind.css";
// 引入icon图标
import "./assets/icons/iconfont.css";
import "./assets/icons/iconfont.js";

import App from "./App.vue";
import router from "./router";
import { setupRouterGuard } from "@/router/guard/index";
import { setupStore } from "@/stores";
import { useUserStoreHook } from "@/stores/modules/user";
import { initRouter } from "@/router/utils";
import { useKBStoreHook } from "@/stores/modules/KB";
import { storageSession } from "@pureadmin/utils";
import { sessionKey, type DataInfo } from "@/utils/auth";

import { MotionPlugin } from "@vueuse/motion";

import { injectResponsiveStorage } from "@/utils/responsiveStorage";
import { getServerConfig } from "./config";

const app = createApp(App);

// 自定义指令
import * as directives from "./directives";

Object.keys(directives).forEach((key) => {
  app.directive(key, (directives as { [key: string]: Directive })[key]);
});

import IconifyIconOffline from "@/components/CommonIcons/IconifyIconOffline";
import IconifyIconOnline from "@/components/CommonIcons/IconifyIconOnline";

app.component("IconifyIconOffline", IconifyIconOffline);
app.component("IconifyIconOnline", IconifyIconOnline);

/* ElementPlus */
import ElementPlus, { ElMessage } from "element-plus";
import zhCn from "element-plus/dist/locale/zh-cn.mjs";
// import "element-plus/dist/index.css";
app.use(ElementPlus, { locale: zhCn });

/* Avue */
import Avue from "@smallwei/avue";
import "@smallwei/avue/lib/index.css";
import { http } from "@/utils/http/index";

app.use(Avue, { size: "default", axios: http.getAxios() });

setupStore(app);

// 引入无界
import WujieVue from "wujie-vue3";

const { bus, setupApp, preloadApp, destroyApp } = WujieVue;
app.use(WujieVue as any);

app.config.warnHandler = () => null; // 去除黄色警告

const bootstrap = async () => {
  try {
    localStorage.removeItem("responsive-layout");
    localStorage.removeItem("responsive-configure");

    const config = await getServerConfig(app);
    const userInfo = storageSession().getItem<DataInfo<number>>(sessionKey);
    console.log("main", config, location.href);
    if (location.href.split("#")[1] != "/login" && !userInfo) {
      // 没有登录且不是访问原始登录界面的时候走格尔单点登录逻辑
      const res = await useUserStoreHook().loginSsoByUsername();
      console.log(res, "res");
      if (res) {
        useKBStoreHook().fetchTyperel();
        useKBStoreHook().fetchTags();
        // 获取后端路由
        initRouter();
      }
    }
    app.use(router);
    setupRouterGuard(router);
    // debugger;
    await router.isReady();
    injectResponsiveStorage(app, config);

    // 处理保活模式
    if (import.meta.env.MODE == "development") {
      setupApp({
        name: "bdp",
        url: "http://localhost:8850/web/bdp/",
        exec: true,
        alive: true,
      });
      // setupApp({
      //   name: "fdp",
      //   url: "http://localhost:8848/web/fdp/",
      //   exec: true,
      //   alive: true,
      // });
      setupApp({
        name: "sys",
        url: "http://localhost:8848/web/sys/",
        exec: true,
        alive: true,
      });
    } else {
      if (config.setupApp) {
        for (let i = 0; i < config.setupApp.length; i++) {
          setupApp({
            name: config.setupApp[i].name,
            url: location.origin + config.setupApp[i].url,
            exec: true,
            alive: true,
          });
        }
      }
    }
  } catch (error) {
    console.log(error);
    ElMessage.warning("配置文件加载失败！");
  } finally {
    app.use(MotionPlugin);
    app.mount("#app");
  }
};

bootstrap();
